package com.hong.taotaomall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hong.taotaomall.common.CustomException;
import com.hong.taotaomall.entity.Category;
import com.hong.taotaomall.entity.Goods;
import com.hong.taotaomall.entity.GoodsSet;
import com.hong.taotaomall.mapper.CategoryMapper;
import com.hong.taotaomall.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hong.taotaomall.service.IGoodsService;
import com.hong.taotaomall.service.IGoodsSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品及套装分类 服务实现类
 * </p>
 *
 * @author hong
 * @since 2023-03-25
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IGoodsSetService goodsSetService;


    @Override
    public void remove(Long ids) {
        LambdaQueryWrapper<Goods> goodsLqw = new LambdaQueryWrapper<>();

        goodsLqw.eq(Goods::getCategoryId,ids);

        long count = goodsService.count(goodsLqw);

        if(count >0){
            throw new CustomException("当前分类关联了套装，不能删除");
        }

        LambdaQueryWrapper<GoodsSet> setLqw = new LambdaQueryWrapper<>();
        setLqw.eq(GoodsSet::getCategoryId,ids);
        long count1 = goodsSetService.count(setLqw);

        if(count1 >0){
            throw new CustomException("当前分类关联了套装，不能删除");
        }
        super.removeById(ids);
    }
}
